package be.kul.osgi.ds.role2;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;

import org.eclipse.core.runtime.FileLocator;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleReference;

import be.kul.osgi.ds.irole.IRole;
import be.kul.osgi.ds.irole.ITask;
import be.kul.osgi.ds.rmi.RMIConnection;
import be.kul.osgi.ds.xmlstreamservice.XMLStreamService;

public class Role implements IRole{

	public XMLStreamService XMLStreamservice;

	public ArrayList<ITask> tasks;
	public RMIConnection rmi;
	public TaskState currentTaskState;
	
	
	public ArrayList<ITask> getTasks() {
		return tasks;
	}

	public ITask makeTask(String xml){
		return XMLStreamservice.readClass(xml, Task.class);
	}

	public void addTask(Task t){
		tasks.add(t);
	}
	
	
	
	public XMLStreamService getXMLStreamservice() {
		return XMLStreamservice;
	}

	public Boolean Committed;
	
	
	public void activate(BundleContext c){
	
	}
	
	public Role()
	{
		System.out.println("role made");
		tasks = new ArrayList<ITask>();
		//Task t1 = new Task(10000, 10000, 10.1, 10.1);
		//this.tasks.add(t1);
		//Task t2 = new Task(20000, 20000);
		//this.tasks.add(t2);
	}
	
	public void setXMLStreamService(XMLStreamService service){
		System.out.println("@Role1 : XMLStreamservice is set, Thank you DS!");
		this.XMLStreamservice = service;		
	}
	
	public void unsetXMLStreamService(XMLStreamService service){
		System.out.println("XMLServicecomponent was unset. Why did you do this to me?");
		if (this.XMLStreamservice == service) {
			this.XMLStreamservice = null;
		}		
	}
	
	public void setRMI(RMIConnection rmi){
		System.out.println("RMIcomponent was set. Thank you DS!");
		this.rmi = rmi;	
		
	}
	
	public void unsetRMI(RMIConnection rmi){
		System.out.println("RMIcomponent was unset. Why did you do this to me?");
		if (this.rmi == rmi) {
			this.rmi = null;
		}	
	}
	
	@Override
	public void reInit() {
		Bundle bundle = BundleReference.class.cast(this.getClass().getClassLoader()).getBundle();		
		URL data = bundle.getResource("/state/state.xml");
		File g;
		String path ;
		XMLStreamService xmlservice = new XMLStreamService();
		try {
			g = new File(FileLocator.toFileURL(data).toURI());
			path = g.getAbsolutePath();
			System.out.println(g);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}


	@Override
	public void execution(ITask t, String agent) {
		Task task = (Task) t;
		this.rmi.goDropoff(agent, task.getID(), task.getX(), task.getY());
	}

	@Override
	public boolean isDone() {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public ITask makeTask(String ID, long duration, long due, double x, double y) {
		Task t1 = new Task(ID, duration, due, x, y);
		return t1;
	}
	
	public TaskState makeTaskState(String ID, boolean pickedup, boolean done, double x, double y){
		return new TaskState(ID, pickedup, done, x, y);
	}

	public void setTaskState(String ID, boolean pickedup, boolean done, double x, double y){
		this.currentTaskState = new TaskState(ID, pickedup, done, x, y);
	}
	
	public TaskState getTaskState(String ID, boolean pickedup, boolean done, double x, double y){
		return this.currentTaskState;
	}

	@Override
	public void execution(ITask t, String agent, String CurrentTaskState) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getCurrentTaskState(String agent) {
		double x = this.rmi.getX(agent);
		double y = this.rmi.getY(agent);
		String id = this.rmi.isBusyWith(agent);
		boolean pickedup = this.rmi.getIsLoaded(agent);
		return this.XMLStreamservice.toXML(new TaskState(id, pickedup, false, x, y));
	}
	
}


